package edu.gatech.volt2.drdoommgt.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.ui.RectangleInsets;

import edu.gatech.volt2.drdoommgt.domain.Patient;
import edu.gatech.volt2.drdoommgt.domain.TreatmentRecord;

public class PatientChartPanel extends JPanel {

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) 
	{
//		ArrayList<TreatmentRecord> tr = new ArrayList<TreatmentRecord>();
//		for (int i = 0; i <10; i++)
//		{
//			Calendar c = Calendar.getInstance();
//			c.add(Calendar.DATE, i*5);
//			TreatmentRecord t = new TreatmentRecord();
//			t.setDate(c);
//			VitalSigns v = new VitalSigns();
//			v.setWeight((int)(Math.random() * 100) + 100);
//			t.setVital(v);
//			tr.add(t);
//		}
//		
//		Patient p = new Patient(new Name("Test", "User"));
//		PatientInfo pi = new PatientInfo();
//		pi.setMedHistory(tr);
//		p.setInfo(pi);
//		
//		PatientChart frame = new PatientChart(p);
//		
//		frame.setVisible(true);
	}


	private JTabbedPane tabbedPane;
	

	/**
	 * Create the frame.
	 */
	public PatientChartPanel() {
		
		this.setLayout(new BorderLayout());
		
		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		this.add(tabbedPane, BorderLayout.CENTER);
		
	}

	
	public JFreeChart createWeightChart(Patient p)
	{
		
		List<TreatmentRecord> trList = p.getTreatmentRecords();
		TimeSeries weightSeries = new TimeSeries("");
		for(TreatmentRecord tr : trList)
		{
			weightSeries.add(new Day(tr.getDate().getTime()), Math.round(tr.getVital().getWeight()));
		}
		
		
		TimeSeriesCollection dataset = new TimeSeriesCollection();
		dataset.addSeries(weightSeries);
		
		JFreeChart chart = ChartFactory.createTimeSeriesChart("", 
				"Date", "Weight (lbs.)", dataset, false, false, false);
		
		chart.setBackgroundPaint(Color.white);
		XYPlot plot = (XYPlot) chart.getPlot();
		plot.setNoDataMessage("No Data Yet");
		plot.setBackgroundPaint(Color.lightGray);
		plot.setDomainGridlinePaint(Color.white);
		plot.setRangeGridlinePaint(Color.white);
		plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
		plot.setDomainCrosshairVisible(false);
		plot.setRangeCrosshairVisible(false);
		
		XYItemRenderer r = plot.getRenderer();
		
	    if (r instanceof XYLineAndShapeRenderer) {
	        XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
	        renderer.setBaseShapesVisible(true);
	        renderer.setBaseShapesFilled(true);
	        renderer.setDrawSeriesLineAsPath(true);
	    }
	    
	    DateAxis axis = (DateAxis) plot.getDomainAxis();
	    axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));

        return chart;
	}
	
	
	public JFreeChart createHeightChart(Patient p)
	{
		
		List<TreatmentRecord> trList = p.getTreatmentRecords();
		TimeSeries weightSeries = new TimeSeries("");
		for(TreatmentRecord tr : trList)
		{
			weightSeries.add(new Day(tr.getDate().getTime()), tr.getVital().getHeightFeet()*12+tr.getVital().getHeightInches());
		}
		
		
		TimeSeriesCollection dataset = new TimeSeriesCollection();
		dataset.addSeries(weightSeries);
		
		JFreeChart chart = ChartFactory.createTimeSeriesChart("", 
				"Date", "Height (in)", dataset, false, false, false);
		
		chart.setBackgroundPaint(Color.white);
		XYPlot plot = (XYPlot) chart.getPlot();
		plot.setNoDataMessage("No Data Yet");
		plot.setBackgroundPaint(Color.lightGray);
		plot.setDomainGridlinePaint(Color.white);
		plot.setRangeGridlinePaint(Color.white);
		plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
		plot.setDomainCrosshairVisible(false);
		plot.setRangeCrosshairVisible(false);
		
		XYItemRenderer r = plot.getRenderer();
		
	    if (r instanceof XYLineAndShapeRenderer) {
	        XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
	        renderer.setBaseShapesVisible(true);
	        renderer.setBaseShapesFilled(true);
	        renderer.setDrawSeriesLineAsPath(true);
	    }
	    
	    DateAxis axis = (DateAxis) plot.getDomainAxis();
	    axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));

        return chart;
	}
	
	
	public JFreeChart createBPChart(Patient p)
	{
		
		List<TreatmentRecord> trList = p.getTreatmentRecords();
		
		TimeSeries systolic = new TimeSeries("");
		TimeSeries diastolic = new TimeSeries("");
		
		for(TreatmentRecord tr : trList)
		{
			String[] parts = tr.getVital().getBP().split("/");
			systolic.add(new Day(tr.getDate().getTime()), Integer.parseInt(parts[0]));
			diastolic.add(new Day(tr.getDate().getTime()), Integer.parseInt(parts[1]));
		}
		
		
		TimeSeriesCollection dataset_sys = new TimeSeriesCollection();
		dataset_sys.addSeries(systolic);
		TimeSeriesCollection dataset_dia = new TimeSeriesCollection();
		dataset_dia.addSeries(diastolic);
		
		JFreeChart chart = ChartFactory.createTimeSeriesChart("", 
				"Date", "Pressure", dataset_sys, false, false, false);
		
		chart.setBackgroundPaint(Color.white);
		XYPlot plot = (XYPlot) chart.getPlot();
		plot.setNoDataMessage("No Data Yet");
		plot.setBackgroundPaint(Color.lightGray);
		plot.setDomainGridlinePaint(Color.white);
		plot.setRangeGridlinePaint(Color.white);
		plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
		plot.setDomainCrosshairVisible(false);
		plot.setRangeCrosshairVisible(false);
		
		plot.setDataset(1, dataset_sys);
		plot.setDataset(2, dataset_dia);

		plot.setRenderer(2, new StandardXYItemRenderer());

		
		XYItemRenderer r = plot.getRenderer();
		
		
	    if (r instanceof XYLineAndShapeRenderer) {
	        XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
	        renderer.setBaseShapesVisible(true);
	        renderer.setBaseShapesFilled(true);
	        renderer.setDrawSeriesLineAsPath(true);
	    }
	    
	    DateAxis axis = (DateAxis) plot.getDomainAxis();
	    axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));

        return chart;
	}

	public void setPatient(Patient p) {
		
		// Weight
		JFreeChart weightChart = createWeightChart(p);
		JPanel wChartPanel = new ChartPanel(weightChart);
		wChartPanel.setBounds(6, 20, 561, 240);
		this.tabbedPane.addTab("Weight", wChartPanel);
		//Height
		JFreeChart heightChart = createHeightChart(p);
		JPanel hChartPanel = new ChartPanel(heightChart);
		hChartPanel.setBounds(6, 20, 561, 240);
		this.tabbedPane.addTab("Height", hChartPanel);
		//BloodPressure
		JFreeChart pressureChart = createBPChart(p);
		JPanel pChartPanel = new ChartPanel(pressureChart);
		pChartPanel.setBounds(6, 20, 561, 240);
		this.tabbedPane.addTab("Blood Pressure", pChartPanel);
	}
}
